package com.matrix.draft.juc.day7;

import cn.hutool.core.util.RandomUtil;
import com.matrix.draft.juc.util.JucUtil;

import java.util.Arrays;
import java.util.concurrent.CountDownLatch;
import java.util.stream.IntStream;

/**
 * 使用CountDownLatch模拟10个加载进度画面
 */
public class TestCountDownLatch {
    public static void main(String[] args) throws Exception {
        loadPlayers(10);
    }

    public static void loadPlayers(int nPlayers) throws Exception {
        CountDownLatch latch = new CountDownLatch(nPlayers);
        String[] progressBars = new String[nPlayers];
        IntStream.range(0, nPlayers).forEach(i -> {
            new Thread(() -> {
                IntStream.range(0, 101).forEach(j -> {
                    JucUtil.sleep(RandomUtil.randomInt(0, 100));
                    progressBars[i] = j + "%";
                    System.out.print("\r"+Arrays.toString(progressBars));
                });
                latch.countDown();
            }).start();
        });
        latch.await();
        System.out.println("\n欢迎来到英雄联盟, 敌军还有30秒到达战场...");
    }
}
